//
//  TJMerchantPersonalCenterController.m
//  BaoDian
//
//  Created by mac on 2024/7/27.
//

#import "TJMerchantPersonalCenterController.h"
#import "TJIconTitleCellButton.h"
#import "TJMineInfoItem.h"
#import "TJMineItem.h"
#import "TJButton.h"
#import "TJUIAlertController.h"
#import "TJModifyPOIController.h"
#import "TJMerchantInfoController.h"

@interface TJMerchantPersonalCenterController ()


@property (nonatomic, weak) TJMineInfoItem *IdItem;
@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UILabel *nicknameLabel;
@property (nonatomic, weak) TJMineInfoItem *createTimeItem;
@property (nonatomic, weak) TJMineInfoItem *expirationTimeItem;


@end

@implementation TJMerchantPersonalCenterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIScrollView *scrollView = [UIScrollView addToView:self.view];
    scrollView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - TabBarHeight);
    CGSize contentSize = scrollView.bounds.size;
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    scrollView.contentSize = contentSize;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.alwaysBounceVertical = YES;
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    
    UIView *contentView = [[UIView alloc] init].addTo(scrollView);
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
        make.width.offset(contentSize.width);
        make.height.offset(contentSize.height);
    }];
    
    UIView *infoView = [UIView addToView:contentView];
    infoView.backgroundColor = UIColor.tj_1E1F23Color;
    infoView.addCornerRadius(12);
    //
    //    TJUserInfo.avatar = @"https://img2.baidu.com/it/u=3246454333,1710591245&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500";
    //    TJUserInfo.nickname = @"秋天落叶生长";
    
    UIImageView *avatarImageView = [UIImageView addToView:contentView];
    avatarImageView.addCornerRadius(8);
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(36);
        make.width.height.offset(60);
        make.top.equalTo(infoView).offset(-9);
    }];
    
    UILabel *nicknameLabel = [UILabel lableWithSuperView:infoView font:UIFontSemiboldMake(18) color:UIColor.whiteColor title:TJUserInfo.nickname textAlignment:NSTextAlignmentLeft];
    [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(14);
        make.left.equalTo(avatarImageView.mas_right).offset(12);
        make.right.mas_lessThanOrEqualTo(-56);
    }];
    
    UIImageView *merchantFlagImageView = [UIImageView addToView:infoView];
    merchantFlagImageView.addCornerRadius(8);
    merchantFlagImageView.image = [UIImage imageNamed:@"merchantFlag"];
    [merchantFlagImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nicknameLabel.mas_right).inset(4);
        make.width.offset(40);
        make.height.offset(18);
        make.centerY.equalTo(nicknameLabel);
    }];
    
    TJMineInfoItem *IdItem = [TJMineInfoItem addToView:infoView];
    IdItem.titleLabel.text = @"会员ID";
    [IdItem.textLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
    }];
    [IdItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatarImageView.mas_bottom).offset(18);
        make.left.offset(0);
        make.right.inset(48);
        make.height.offset(20);
    }];
    
    TJButton *copyButton = [TJButton buttonWithSuperView:infoView font:UIFontMediumMake(12) color:UIColorMake(0x2978F0) title:@"复制"];
    [copyButton addTarget:self action:@selector(didClickCopyButton) forControlEvents:UIControlEventTouchUpInside];
    [copyButton sizeToFit];
    [copyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(16);
        make.centerY.equalTo(IdItem.textLabel);
        make.width.offset(copyButton.tj_width);
        make.height.offset(copyButton.tj_height);
    }];
    
    TJMineInfoItem *createTimeItem = [TJMineInfoItem addToView:infoView];
    createTimeItem.titleLabel.text = @"账号创建时间";
    [createTimeItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(IdItem.mas_bottom).offset(8);
        make.left.right.inset(0);
        make.height.equalTo(IdItem);
    }];
    
    TJMineInfoItem *expirationTimeItem = [TJMineInfoItem addToView:infoView];
    expirationTimeItem.titleLabel.text = @"账号到期间";
    [expirationTimeItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(createTimeItem.mas_bottom).offset(8);
        make.left.right.height.equalTo(createTimeItem);
    }];
    
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.top.offset(KNavBarHight + 10);
        make.bottom.equalTo(expirationTimeItem.mas_bottom).offset(16);
    }];
    
    TJMineItem *taskManagerItem = [TJMineItem addToView:contentView];
    taskManagerItem.titleLabel.text = @"任务管理";
    taskManagerItem.imageView.image = [UIImage imageNamed:@"taskManager"];
    [taskManagerItem addTapGesturesWithTarget:self action:@selector(tapTaskManagerItem)];
    [taskManagerItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.inset(16);
        make.height.offset(70);
        make.top.equalTo(infoView.mas_bottom).inset(20);
        make.width.offset((KScreenWidth - 48) / 3);
    }];
    
    TJMineItem *delegateItem = [TJMineItem addToView:contentView];
    delegateItem.titleLabel.text = @"员工管理";
    delegateItem.imageView.image = [UIImage imageNamed:@"delegate"];
    [delegateItem addTapGesturesWithTarget:self action:@selector(tapDelegateItem)];
    [delegateItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(taskManagerItem.mas_right).offset(8);
        make.top.height.width.equalTo(taskManagerItem);
    }];
    
    TJMineItem *activationCodeItem = [TJMineItem addToView:contentView];
    activationCodeItem.titleLabel.text = @"我的激活码";
    activationCodeItem.imageView.image = [UIImage imageNamed:@"activationCode"];
    [activationCodeItem addTapGesturesWithTarget:self action:@selector(tapActivationCodeItem)];
    [activationCodeItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(16);
        make.top.height.width.equalTo(taskManagerItem);
    }];
    
    TJIconTitleCellButton *settingButton = [TJIconTitleCellButton addToView:contentView];
    settingButton.backgroundColor = UIColor.tj_1E1F23Color;
    [settingButton addTarget:self action:@selector(didClickListButton:) forControlEvents:UIControlEventTouchUpInside];
    settingButton.model = [TJCellModel modelWithBlock:^(TJCellModel * _Nonnull model) {
        model.text = @"设置POI";
        model.identifier = @"POI";
        model.iconName = @"setting_icon";
        model.showArrow = YES;
    }];
    settingButton.bounds = CGRectMake(0, 0, KScreenWidth - 32, 56);
    [settingButton addRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:12];
    [settingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.top.equalTo(activationCodeItem.mas_bottom).inset(20);
        make.height.offset(56);
    }];
    
    TJIconTitleCellButton *userInfoButton = [TJIconTitleCellButton addToView:contentView];
    userInfoButton.backgroundColor = UIColor.tj_1E1F23Color;
    [userInfoButton addTarget:self action:@selector(didClickListButton:) forControlEvents:UIControlEventTouchUpInside];
    userInfoButton.model = [TJCellModel modelWithBlock:^(TJCellModel * _Nonnull model) {
        model.text = @"个人信息";
        model.identifier = @"userInfo";
        model.iconName = @"mine_userInfo_icon";
        model.showArrow = YES;
    }];
    [userInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.top.equalTo(settingButton.mas_bottom);
        make.height.offset(56);
    }];
    
    TJIconTitleCellButton *versionButton = [TJIconTitleCellButton addToView:contentView];
    versionButton.backgroundColor = UIColor.tj_1E1F23Color;
    versionButton.model = [TJCellModel modelWithBlock:^(TJCellModel * _Nonnull model) {
        model.text = @"APP版本";
        model.identifier = @"version";
        model.iconName = @"version_icon";
        model.showArrow = NO;
    }];
    versionButton.bounds = CGRectMake(0, 0, KScreenWidth - 32, 56);
    [versionButton addRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight radius:12];
    [versionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.top.equalTo(userInfoButton.mas_bottom);
        make.height.offset(56);
    }];
    
    UILabel *versionValueLabel = [UILabel lableWithSuperView:versionButton font:UIFontMake(12) color:UIColor.tj_8D9298Color title:[NSString stringWithFormat:@"当前版本v%@", getAppVersion()] textAlignment:NSTextAlignmentLeft];
    [versionValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(16);
        make.centerY.offset(0);
    }];
    
    TJIconTitleCellButton *logoutButton = [TJIconTitleCellButton addToView:contentView];
    logoutButton.backgroundColor = UIColor.tj_1E1F23Color;
    logoutButton.addCornerRadius(12);
    [logoutButton addTarget:self action:@selector(didClickListButton:) forControlEvents:UIControlEventTouchUpInside];
    logoutButton.model = [TJCellModel modelWithBlock:^(TJCellModel * _Nonnull model) {
        model.text = @"退出登录";
        model.identifier = @"logout";
        model.iconName = @"logout_icon";
        model.showArrow = YES;
    }];
    [logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.top.equalTo(versionButton.mas_bottom).inset(10);
        make.height.offset(56);
    }];
    
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    
    contentSize = CGSizeMake(contentSize.width, logoutButton.tj_MaxY + 10);
    scrollView.contentSize = contentSize;
    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(contentSize.height);
    }];
    
    
    _nicknameLabel = nicknameLabel;
    _avatarImageView = avatarImageView;
    _IdItem = IdItem;
    _expirationTimeItem = expirationTimeItem;
    _createTimeItem = createTimeItem;
    
    [self reloadData];
}


- (void)reloadData {
    dispatch_async_sql_queue(^{
        TJUserModel *userModel = [TJUserModel currentAccountModel];
        dispatch_async_main_queue(^{
            self.IdItem.textLabel.text = userModel.Id;
            [self.avatarImageView tj_setImageWithURL:userModel.avatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
            self.nicknameLabel.text = userModel.realName;
            self.createTimeItem.textLabel.text = userModel.createDate;
            self.expirationTimeItem.textLabel.text = userModel.expireDate;
        });
    });
}


- (void)didClickCopyButton {
    [TJTools copyStringToPasteboard:_IdItem.textLabel.text];
    [TJHUD showMessage:@"复制成功"];
}

- (void)tapTaskManagerItem {
    [self.navigationController pushViewControllerWithName:@"TJTaskManagerListController"];
}

- (void)tapDelegateItem {
    [self.navigationController pushViewControllerWithName:@"TJStaffManageController"];
}

- (void)didClickListButton:(TJIconTitleCellButton *)button {
    
    TJCellModel *model = button.model;
    
    if ([model.identifier isEqualToString:@"logout"]) {
        TJUIAlertController *alert = [TJUIAlertController alertControllerWithTitle:nil message:@"您是否确认退出该账号" preferredStyle:TJUIAlertControllerStyleActionSheet];
        [alert addAction:[TJUIAlertAction actionWithTitle:@"确认" style:TJUIAlertActionStyleDefault handler:^(TJUIAlertAction * _Nonnull action) {
            [[NSNotificationCenter defaultCenter] postNotificationName:logoutNotification object:nil];
        }]];
        [alert addAction:[TJUIAlertAction actionWithTitle:@"取消" style:TJUIAlertActionStyleCancel handler:^(TJUIAlertAction * _Nonnull action) {
            
        }]];
        [alert showInViewController:self];
        return;
    }
    
    if ([model.identifier isEqualToString:@"userInfo"]) {
        TJMerchantInfoController *merchantInfoController = [[TJMerchantInfoController alloc] init];
        [merchantInfoController setUpdateInfoBlock:^(NSInteger type, id  _Nonnull value) {
            if (type == 1) {
                [self.avatarImageView tj_setImageWithURL:[value URL] placeholderImage:self.avatarImageView.image];
            } else if (type == 2) {
                self.nicknameLabel.text = value;
            }
        }];
        [self.navigationController pushViewController:merchantInfoController];
        return;
    }
    
    
    if ([model.identifier isEqualToString:@"POI"]) {
        TJModifyPOIController *modifyPOIController = [[TJModifyPOIController alloc] init];
        [modifyPOIController setComplete:^(NSString * _Nonnull poi) {
            dispatch_async_sql_queue(^{
                TJUserModel *userModel = [TJUserModel currentAccountModel];
                userModel.poiSourceInfo = poi;
                [userModel bg_saveOrUpdate];
            });
        }];
        [self presentViewController:modifyPOIController animated:NO completion:nil];
        return;
    }
}

- (void)tapActivationCodeItem {
    [self.navigationController pushViewControllerWithName:@"TJActivationCodeController"];
    
}

- (BOOL)hiddenNavigationBar {
    return YES;
}

- (UIImage *)topBackgroundImage {
    return [UIImage imageNamed:@"mine_bg"];
}

@end
